<template>
  <div class="mod-seckill-shopAuditing">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="searchForm"
        :class="$t('language') === 'language' ? 'en-search-form': 'zh-search-form'"
      >
        <div class="input-row">
          <el-form-item
            prop="merchantName"
            label="企业名称:"
          >
            <el-input
              v-model="searchForm.merchantName"
              clearable
              placeholder="企业名称"
            />
          </el-form-item>

          <el-form-item
            prop="createdBy"
            label="企业管理员:"
          >
            <el-input
              v-model="searchForm.createdBy"
              clearable
              placeholder="企业管理员"
            />
          </el-form-item>

          <el-form-item
            prop="userMobile"
            label="手机号:"
          >
            <el-input
              v-model="searchForm.userMobile"
              clearable
              maxlength="11"
              placeholder="手机号"
            />
          </el-form-item>

          <el-form-item
            prop="mallVersion"
            label="全网推版本:"
          >
            <el-select
              v-model="searchForm.mallVersion"
              clearable
              placeholder="全网推版本"
            >
              <el-option
                v-for="(item, index) in recVer"
                :key="index"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item
            prop="tradeVersion"
            label="全网买版本:"
          >
            <el-select
              v-model="searchForm.tradeVersion"
              clearable
              placeholder="全网买版本"
            >
              <el-option
                v-for="(item, index) in buyVer"
                :key="index"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item
            prop="tradeLevel"
            label="全网买等级:"
          >
            <el-select
              v-model="searchForm.tradeLevel"
              clearable
              placeholder="全网买等级"
            >
              <el-option
                v-for="(item, index) in buyLev"
                :key="index"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item
            label="全网推开通时间:"
          >
            <el-date-picker
              v-model="dateRange"
              type="datetimerange"
              :range-separator="$t('date.tip')"
              :default-time="[new Date(2000, 1, 1, 0, 0, 0),
                              new Date(2000, 2, 1, 23, 59, 59)]"
              value-format="YYYY-MM-DD HH:mm:ss"
              :start-placeholder="$t('date.start')"
              :end-placeholder="$t('date.end')"
            />
          </el-form-item>

          <el-form-item
            label="全网买开通时间:"
          >
            <el-date-picker
              v-model="dateRange2"
              type="datetimerange"
              :range-separator="$t('date.tip')"
              :default-time="[new Date(2000, 1, 1, 0, 0, 0),
                              new Date(2000, 2, 1, 23, 59, 59)]"
              value-format="YYYY-MM-DD HH:mm:ss"
              :start-placeholder="$t('date.start')"
              :end-placeholder="$t('date.end')"
            />
          </el-form-item>

          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('product.search') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch()"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <div class="main-container">
      <div class="table-con audit-shop-table">
        <el-table
          ref="auditShopRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row"
          style="width: 100%"
        >
          <el-table-column
            prop="merchantName"
            label="企业名称"
            min-width="150"
          />

          <el-table-column
            prop="merchantShortName"
            label="企业简称"
            width="100"
          />

          <el-table-column
            prop="createdBy"
            label="企业管理员"
            width="100"
          />

          <el-table-column
            prop="userMobile"
            label="手机号"
            width="120"
          />

          <el-table-column
            prop="mallVersion"
            label="全网推版本"
            width="100"
          >
            <template #default="scope">
              <span class="tag-text">
                {{ recVer.find(d => d.value == scope.row.mallVersion)?.label }}
              </span>
            </template>
          </el-table-column>

          <el-table-column
            prop="tradeVersion"
            label="全网买版本"
            width="100"
          >
            <template #default="scope">
              <span class="tag-text">
                {{ buyVer.find(d => d.value == scope.row.tradeVersion)?.label }}
              </span>
            </template>
          </el-table-column>

          <el-table-column
            prop="tradeLevel"
            label="全网买等级"
            width="100"
          >
            <template #default="scope">
              <span class="tag-text">
                {{ buyLev.find(d => d.value == scope.row.tradeLevel)?.label }}
              </span>
            </template>
          </el-table-column>

          <el-table-column
            prop="mallActivateTime"
            label="全网推开通时间"
            width="200"
          />

          <el-table-column
            prop="tradeActivateTime"
            label="全网买开通时间"
            width="200"
          />

          <el-table-column
            align="center"
            fixed="right"
            :label="$t('publics.operating')"
            width="200"
          >
            <template #default="scope">
              <div class="">
                <!-- 查看 -->
                <div
                  class="default-btn text-btn"
                  @click="onView(scope.row.shopCompanyId)"
                >
                  {{ $t('seckill.view') }}
                </div>
                <!-- 编辑 -->
                <!-- <div
                  class="default-btn text-btn"
                  @click="onEdit(scope.row.shopCompanyId)"
                >
                  {{ $t('coupon.edit') }}
                </div> -->
                <!-- 账号管理 -->
                <!-- <div
                  v-if="isAuth('shop:shopAuditing:audit')"
                  class="default-btn text-btn"
                  @click="shopAccountHandle(scope.row.shopId)"
                >
                  {{ $t('shop.accountManagement') }}
                </div> -->
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>

    <!-- 查看 -->
    <addOrUpdate ref="addOrUpdateRef" />
  </div>
</template>

<script setup>
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import { isAuth } from '@/utils/index.js'
import addOrUpdate from '../add-or-update/index.vue'
import { recVer, buyVer, buyLev } from '../../constant'

const searchForm = reactive({})

onMounted(() => {
  getDataList()
})

let tempSearchForm = null // 保存上次点击查询的请求条件
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const dateRange = ref([])
const dateRange2 = ref([])
let theParams = null // 保存上次点击查询的请求条件
const dataList = ref([])
const getDataList = (pageParam, newData = false) => {
  pageParam = pageParam || page
  if (newData || !tempSearchForm) {
    tempSearchForm = {
      current: pageParam.currentPage,
      size: pageParam.pageSize,
      mallBeginQueryTime: dateRange.value ? dateRange.value[0] : null, // 开始时间
      mallEndQueryTime: dateRange.value ? dateRange.value[1] : null, // 结束时间
      tradeBeginQueryTime: dateRange2.value ? dateRange2.value[0] : null, // 开始时间
      tradeEndQueryTime: dateRange2.value ? dateRange2.value[1] : null // 结束时间
    }
    theParams = JSON.parse(JSON.stringify(searchForm))
  } else {
    tempSearchForm.current = pageParam.currentPage
    tempSearchForm.size = pageParam.pageSize
  }
  theParams.status = '1'
  theParams.sort = 'shopCompanyId'
  http({
    url: http.adornUrl('/platform/shopCompany/page'),
    method: 'get',
    params: http.adornParams(Object.assign(tempSearchForm, theParams))
  }).then(({ data }) => {
    dataList.value = data.records
    page.total = data.total
    page.currentPage = data.current

    // 末尾页数据为空重新请求
    if (!dataList.value.length && page.currentPage > 1) {
      page.currentPage = 1
      getDataList()
    }
  })
}

const shopAccountVisible = ref(false)
const shopAccountRef = ref(null)
// 账号管理
const shopAccountHandle = (id) => {
  shopAccountVisible.value = true
  nextTick(() => {
    shopAccountRef.value?.init(id)
  })
}

// 清空自定义数据
const searchReset = () => {
  dateRange.value = []
  dateRange2.value = []
}

const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}

const auditShopRef = ref(null)

const searchFormRef = ref(null)
/**
 * 重置表单
 */
const onResetSearch = () => {
  searchFormRef.value.resetFields()
  searchReset()
}

const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList()
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList()
}

const addOrUpdateRef = ref(null)
const onView = (id) => {
  addOrUpdateRef.value?.init(id)
}
</script>

<style lang="scss" scoped>
.table-con {
  position: relative;
}

:deep(.export-load) {
  top: -50% !important;
}
:deep(.el-table__fixed-right::before) {
  z-index: 0;
}
</style>
